package main

import (
	"flag"
	"fmt"
	// "net/http"

	"mall/service/forum/api/internal/config"
	"mall/service/forum/api/internal/handler"
	"mall/service/forum/api/internal/svc"

	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/rest"
)

var configFile = flag.String("f", "etc/forum.dev.yaml", "the config file")

func main() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c)

	// server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(
	// 	// 设置 http header
	// 	func(header http.Header) {
	// 		header.Set("Access-Control-Allow-Origin", "*")
	// 		header.Set("Access-Control-Allow-Methods", "GET, HEAD, POST, PATCH, PUT, DELETE, OPTIONS")
	// 	},
	// 	// 不允许地址返回指定数据
	// 	func(writer http.ResponseWriter) {
	// 		writer.Write([]byte("not allow"))
	// 	},
	// 	// 允许跨域地址
	// 	c.CROSConf.Address,
	// ))
	server := rest.MustNewServer(c.RestConf, rest.WithCors(c.CROSConf.Address))
	defer server.Stop()

	ctx := svc.NewServiceContext(c)
	handler.RegisterHandlers(server, ctx)

	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
	server.Start()
}
